package com.example.administrator.yuejian2.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.example.administrator.yuejian2.R;
import com.example.administrator.yuejian2.RetrofitListener.RetrofitListener;
import com.example.administrator.yuejian2.adapter.MyTrainOrderAdapter;
import com.example.administrator.yuejian2.bean.DeleteOrderBean;
import com.example.administrator.yuejian2.bean.TrainOrderBean;
import com.example.administrator.yuejian2.model.DeleteMyTrainOrderModel;
import com.example.administrator.yuejian2.model.MyTrainOrderModel;

import java.util.List;

public class MyOrderActivity extends BaseActivity implements MyTrainOrderAdapter.IonSlidingViewClickListener {


    private RecyclerView mRecyclerView;
    private RetrofitListener<List<TrainOrderBean>> listRetrofitListener;
    private MyTrainOrderAdapter mAdapter;
    private RetrofitListener<DeleteOrderBean> listener;
    private SharedPreferences sp;
    private String user_id,o_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order);
        sp = getSharedPreferences("user_id", Context.MODE_PRIVATE);
        user_id = sp.getString("userid", null);

        mRecyclerView = (RecyclerView) findViewById(R.id.my_train_order_list_recyclerview);

        //设置布局管理器
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));//设置布局管理器
        mRecyclerView.setAdapter(mAdapter = new MyTrainOrderAdapter(this));//设置适配器
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());//设置控制Item增删的动画
        listRetrofitListener=new RetrofitListener<List<TrainOrderBean>>() {
            @Override
            public void onSuccess(List<TrainOrderBean> list, int flag) {
                mAdapter.getMyTrainOrderListData(list);
            }

            @Override
            public void onFail() {

            }
        };
        listener=new RetrofitListener<DeleteOrderBean>() {
            @Override
            public void onSuccess(DeleteOrderBean deleteOrderBean, int flag) {
                if (deleteOrderBean.getSuccess().equals("1"))
                {
                    Toast.makeText(MyOrderActivity.this, "删除成功", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFail() {

            }
        };
        getMyTrainOrder();

    }

    private void getMyTrainOrder()
    {
        MyTrainOrderModel myTrainOrderModel=new MyTrainOrderModel();
        myTrainOrderModel.getMyTrainOrderModel(user_id,listRetrofitListener);
    }
    private void deleteMyTrainOrder()
    {
        DeleteMyTrainOrderModel deleteMyTrainOrderModel=new DeleteMyTrainOrderModel();
        deleteMyTrainOrderModel.deleteTrainOrder(o_id,user_id,listener);
    }
    @Override
    public void onItemClick(View view, int position) {
        Toast.makeText(MyOrderActivity.this, "123", Toast.LENGTH_LONG).show();
        Intent intent=new Intent(MyOrderActivity.this,TrainOrderDetailActivity.class);
        String id=MyTrainOrderAdapter.id;
        intent.putExtra("null",id);
        startActivity(intent);
    }

    @Override
    public void onDeleteBtnCilck(View view, int position) {

       // Toast.makeText(MyOrderActivity.this, "删除", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onSetBtnCilck(View view, int position) {

        Toast.makeText(MyOrderActivity.this, "删除", Toast.LENGTH_LONG).show();
        o_id=MyTrainOrderAdapter.id;
        deleteMyTrainOrder();
        mAdapter.removeData(position);

    }
}
